/**
  ******************************************************************************
  * @author     Chris
  * @since      2024/6/10 12:38
  *
  * @file       hd_oled_protocol_i2c.c
  * @brief      OLED IIC Communication Protocol Hardware Driver.
  *
  * @note       This file contains the IIC Communication Protocol hardware
  *             driver for the OLED.
  *
  * @warning    None.
  ******************************************************************************
  * Change Logs:
  *   Date           Author       Notes
  *   2024-06-10     Chris        the first version
  *
  ******************************************************************************
  */

#include "hd_oled_protocol_i2c.h"


/**
 * IIC GPIO init
 *
 * @param super
 */
static void OLEDProtocolI2CSimulate_init(OLEDProtocol *super) {

    /**
     *  TODO IIC can init in here or main() function
     *  It's not suitable to init IIC in here, when we have more than one i2c device,
     *  because it can be repeatable. It's better to init IIC in main() function.
     */
    // OLEDProtocolI2C *this = (OLEDProtocolI2C *) super;
    // this->i2c->init(this->i2c);
}


static void OLEDProtocolI2CSimulate_write(OLEDProtocol *super,
                                          uint8_t salveAddress, uint16_t regAddress, uint8_t *dat, uint8_t len) {
    OLEDProtocolI2C *this = (OLEDProtocolI2C *) super;
    this->i2c->send(this->i2c, salveAddress, regAddress, dat, len);
}

/**
 * Constructor of {@link OLEDProtocolI2C} Object
 *
 * @param i2c
 * @return
 */
static OLEDProtocolI2C *build(IIC *i2c) {
    OLEDProtocolI2C *opi = malloc(sizeof(OLEDProtocolI2C));

    Driver_add(opi, GEN_PERM);

    opi->i2c                = i2c;

    OLEDProtocol *op        = OLEDProtocolBuilder.build();
    opi->oledProtocol       = *op;
    opi->oledProtocol.init  = &OLEDProtocolI2CSimulate_init;
    opi->oledProtocol.write = &OLEDProtocolI2CSimulate_write;

    return opi;
}

const struct OLEDProtocolI2CClass OLEDProtocolI2CBuilder = {.build = &build};

